﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;


namespace CRM.Web.BusinessDivisionManage
{
    public partial class Modify : Common.PageBase
    {

        T_BUSINESSDIVISION_BLL bll = new T_BUSINESSDIVISION_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
            //PageHelper.SetPopCalender(this.Page, txtCREATETIME);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIME);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                ddlSTATUSBinding();
                ddlISVIEWBinding();
                ddlISVIEWEDBinding();

                pageDataInit();
            }
        }

        #region 页面初始化数据绑定

        private void ddlSTATUSBinding()
        {
            ddlSTATUS.Items.Clear();
            EnumHelper.FillListControl(ddlSTATUS, typeof(Status));
        }



        private void ddlISVIEWBinding()
        {
            ddlISVIEW.Items.Clear();
            EnumHelper.FillListControl(ddlISVIEW, typeof(IsView));
        }
        private void ddlISVIEWEDBinding()
        {
            ddlISVIEWED.Items.Clear();
            EnumHelper.FillListControl(ddlISVIEWED, typeof(IsViewed));
        }




        




        #endregion

        private void pageDataInit()
        {
            string _businessdivisionid = String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]) ? "" : Request.QueryString["BUSINESSDIVISIONID"].Trim();

            if (!String.IsNullOrEmpty(_businessdivisionid))
            {
                T_BUSINESSDIVISION_Entity entity = bll.GetEntity("BUSINESSDIVISIONID", _businessdivisionid);

                PageHelper.SetControlValue<T_BUSINESSDIVISION_Entity>(this.Page, entity);
                lblTitle.Text = "事业部修改";
            }
            else
            {
                lblTitle.Text = "事业部添加";
            }
            txtCREATEPEOPLE.Enabled = false;
            txtCREATEPEOPLEID.Enabled = false;
            txtCREATETIME.Enabled = false;
            txtLASTMODIFYPEOPLE.Enabled = false;
            txtLASTMODIFYPEOPLEID.Enabled = false;
            txtLASTMODIFYTIME.Enabled = false;


        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _businessdivisionid = String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]) ? "" : Request.QueryString["BUSINESSDIVISIONID"].Trim();


            T_BUSINESSDIVISION_Entity entity = bll.GetEntity("BUSINESSDIVISIONID", _businessdivisionid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_BUSINESSDIVISION_Entity>(this.Page, ref entity);
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;

                try
                {
                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "list.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {
                        PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_BUSINESSDIVISION_Entity();

                PageHelper.GetControlValue<T_BUSINESSDIVISION_Entity>(this.Page, ref entity);
                entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.CREATETIME = DateTime.Now;
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;

                try
                {
                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "list.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }
                }
                catch (Exception ex)
                {
                        PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
                }

            }
        }
    }
}
